package com.turing.data.controller;

import com.turing.common.response.ResponseCode;
import com.turing.common.response.ResponseMessage;
import com.turing.common.response.ResponseVO;
import com.turing.common.util.*;
import com.turing.data.model.domain.element.Element;
import com.turing.data.vo.in.element.ReviewContentVo;
import com.turing.data.vo.in.review.ForecastVO;
import com.turing.orm.handler.DateTableNameParser;
import com.turing.data.model.domain.element.Template;
import com.turing.data.model.domain.review.ModelContent;
import com.turing.data.model.domain.review.ReviewContent;
import com.turing.data.service.element.ElementService;
import com.turing.data.service.element.TemplateService;
import com.turing.data.service.review.ReviewContentService;
import com.turing.data.service.review.ReviewOperationService;
import com.turing.data.service.review.ReviewService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("op")
public class OperationController {

    @Autowired
    private ReviewOperationService reviewOperationService;

    @Autowired
    private ElementService elementService;

    @Autowired
    private ReviewService reviewService;

    @PostMapping("finish")
    public ResponseVO<Object> finishReviewTask(@RequestParam("parentTaskSn") String taskSn) {
        return ResponseUtil.success(reviewOperationService.finishReviewTask(taskSn));
    }

    @PostMapping("uploadModelContent")
    public ResponseVO<Boolean> uploadModelContent(@RequestBody List<ModelContent> contents) {
        boolean result = reviewOperationService.uploadModelContent(contents);
        return new ResponseVO<>(result, ResponseCode.SUCCESS, ResponseMessage.SUCCESS, 0);
    }

    @PostMapping("updateContent")
    public ResponseVO<Object> updateReviewContent(@RequestBody ReviewContentVo reviewContentVo) {
        return ResponseUtil.success(reviewOperationService.updateReviewContent(reviewContentVo));
    }

    @PostMapping("addSingle")
    public ResponseVO<Object> addSingle(@RequestBody ReviewContentVo reviewContentVo) {
        return ResponseUtil.success(reviewOperationService.addSingle(reviewContentVo));
    }

    @PostMapping("delSingle")
    public ResponseVO<Object> delSingle(@RequestBody ReviewContentVo content) {
        return ResponseUtil.success(reviewOperationService.delSingle(content));
    }

    @PostMapping("addBatch")
    public ResponseVO<Object> addBatch(@RequestBody List<ReviewContentVo> contents) {
        return ResponseUtil.success(reviewOperationService.addBatch(contents));
    }

    @PostMapping("delBatch")
    public ResponseVO<Object> delBatch(@RequestBody List<ReviewContentVo> contents) {
        return ResponseUtil.success(reviewOperationService.delBatch(contents));
    }

    @PostMapping("addRow")
    public ResponseVO<Object> addRow(@RequestBody List<ReviewContentVo> contents) {
        return ResponseUtil.success(reviewOperationService.addRow(contents));
    }

    @PostMapping("delRow")
    public ResponseVO<Object> delRow(@RequestBody List<ReviewContentVo> contents) {
        return ResponseUtil.success(reviewOperationService.delRow(contents));
    }

    @PostMapping("saveForecastResult")
    public ResponseVO<Object> saveForecastResult(@RequestBody Map<String,List<ForecastVO>> forecastEntity , @RequestParam("taskSn") String taskSn,
                                                 @RequestParam("templateId") String templateSn, @RequestParam("parentTaskSn") String parentTaskSn ){

        List<Element> elements = elementService.getElementsByTemplateSn(templateSn,0);
        reviewOperationService.saveModelContents(forecastEntity, taskSn, parentTaskSn, elements);
        return ResponseUtil.success();

    }

    @PostMapping("populateEmptyElements")
    public ResponseVO<Object> populateEmptyElements( @RequestParam("parentTaskSn") String taskSn, @RequestParam("templateId") String templateSn ){
        return ResponseUtil.success(reviewOperationService.populateEmptyElements(taskSn,templateSn));
    }

    @PostMapping("exportElements")
    public void exportElements(@RequestParam("taskSn") String taskSn, @RequestParam("templateId") String templateId, HttpServletResponse response ) throws IOException {
        reviewService.exportData( taskSn, templateId, response);;
    }

    @GetMapping("exportElements1")
    public void exportElements1(@RequestParam("taskSn") String taskSn, @RequestParam("templateId") String templateId, HttpServletResponse response ) throws IOException {
        reviewService.exportData( taskSn, templateId, response);;
    }

    @PostMapping("removeTasks")
    public ResponseVO<Object> removeTasks(@RequestBody List<String> parentTaskSns){
        return ResponseUtil.success(reviewOperationService.removeTasks(parentTaskSns));
    }

    @PostMapping("exportElementsWord")
    public void exportElementsWord(@RequestParam("taskSn") String taskSn, @RequestParam("templateId") String templateId, HttpServletResponse response ) throws ExecutionException, InterruptedException {
        reviewService.exportElementsWord(taskSn,templateId,response);
    }

}
